// SCENARIO SCRIPT

// This is the special script for your entire scenario. It contains
// special encounters and code accessable from anywhere in the scenario. it also
// contains the code that initializes important special things in the
// scenario (like shops and names and descriptions of special items).

// You can create your own states, but you should give all of them numbers greater than
// or equal to 10.

beginscenarioscript;

variables;

int i,j,k;
int level, modifier;

body;

beginstate LOAD_SCEN_STATE;
// This is the state that is called every time the scenario is loaded,
// even when a save file in the scenario is loaded. Some things that should go here:
//    Names and descriptions of special items.
//    Names and effects of custom special abilities.

//Good quests
init_quest(1,"Secure escape tunnels.","You have been ordered to ensure that the people of New Haven can escape down the emergency escape tunnels. There are three tunnels you need to secure - one in the NW, one in the SW and one in the SE.");
init_quest(2,"Get to Town Hall.","Dervish Stieger needs you to get to Town Hall and help fend off the attackers as soon as possible. Time is of the essence - you can't sustain an attack for long...");

//Bad quests
init_quest(3,"Block escape tunnels.","You have been told to prevent the people of New Haven from escaping down the emergency escape tunnels. There are three tunnels you need to block in order to do this - one in the NW, one in the SW and one in the SE.");
init_quest(4,"Kill the Mayor.","Steven needs you to get to Town Hall and help kill Mayor Rourke and his bodyguards. Time is of the essence - you can't sustain an attack for long...");
break;

beginstate START_SCEN_STATE;
// This is the state that is called only once at the very beginning of 
// the scenario. Some things that should go here:
//    The stuff in shops.
//    Creating horses and boats.
set_creature_type_level(6,2);

reset_dialog();
add_dialog_str(0,"There are two paths, one leading left, the other leading right. Which path do you wish to travel down?",0);
add_dialog_choice(0,"The left path."); //Good - help evacuate
add_dialog_choice(1,"The right path."); //Evil - help attack
if(run_dialog(1) == 1) { //Left
	message_dialog("You travel down the left path...","");
	set_flag(250,0,1);
	
	reset_dialog();
	add_dialog_str(0,"You are an Imperial soldier, fresh out of military school, and two months into your first placement - a city called New Haven.",0);
	add_dialog_str(1,"New Haven used to be a quiet place, not particularly important or notable, but a pleasant place to live. Recently though, a small group of bandits revealed themselves nearby, and began launching attacks on the city walls.",0);
	add_dialog_str(2,"The attacks are not effective physically, but have succeeded in spreading panic throughout the city's population. Mayor Rourke, the city's leader, has taken it upon himself to rid the city of the bandits, although so far he has met no success.",0);
	add_dialog_str(3,"Your duties mostly alternate between patrolling the town walls, looking for bandits, and guarding Mayor Rourke. You aren't yet deemed experienced enough to go outside and hunt down the bandits, although hopefully that'll change soon...",0);
	add_dialog_str(3,"You prepare for bed one night, after a blissfully attack-free day, being told you're finally being allowed to go out tomorrow. Sleep comes quickly...",0);
	run_dialog(1);
	
	message_dialog("You are jolted from your sleep by a huge, earth-shattering explosion to the south. The screams of women and children fill the air. Jumping out of bed, you see your Commanding Officer burst in from her office.","_Quick, get up! You,_ she points to a small group of men, _go and find out what's happening!_ She turns to you. _You! Wake the fuck up! We don't have time to mess about! I have orders for you! Move it!_");
	message_dialog("(You may want to rename and change the graphic of your PC in order to reflect your status and skills, although this is in no way required of you. Also, note that any saved game entering this scenario is forfeit. See the ReadMe file for more information.)","");
	
	i = 0;
	k = 0;
	while(char_ok(i) == 0) {
		i = i + 1;
		}
				
	while(k <= 3) {
		j = 39;
		if(char_ok(k) == 1) {
			while(j >= 0) {
				destroy_char_item(k,j);
				j = j - 1;
			}
			if(k != i) {
				erase_char(k);
			}
		}
		k = k + 1;
	}
	
	set_flag(250,1,i);
	
	level = (1 - get_level(i));
	set_level(i,1);
				
	modifier = 100;
	while(level < 1) {
		change_char_xp(i,-25 * modifier);
		level = level + 1;
	}
			
	modifier = 0;
	while(modifier <= 22) {
		set_char_trait(i,modifier,0);
		modifier = modifier + 1;
	}
	
	modifier = 0;
	while(modifier <= 19) {
		if(get_spell_level(i,0,modifier) > 0)
			change_spell_level(i,0,modifier,-1 * get_spell_level(i,0,modifier));
		if(get_spell_level(i,1,modifier) > 0)
			change_spell_level(i,1,modifier,-1 * get_spell_level(i,1,modifier));
		modifier = modifier + 1;
	}
	
	change_pc_skill_pts(i,-1 * (get_char_skill_pts(i)));
	
	j = 0;
	while (j < 32) {
		modifier = get_stat(i,j);
		alter_stat(i,j,-1 * modifier);
		j = j + 1;
	}
	
	alter_stat(i,4,6); //Melee Weapons
	alter_stat(i,6,3); //Bows
	alter_stat(i,8,3); //Hardiness
	alter_stat(i,9,3); //Defence
	alter_stat(i,21,2); //Blademaster
	alter_stat(i,13,5); //Arcane Lore

//Clean up, and misc.
			
	revive_party();
	change_coins(-1 * coins_amount()); //Make them poor!
	force_start_day(-1); //No calender, please - we're british
	reward_give(18);
	reward_give(20);
	reward_give(114);
	
} else { //If evil

	message_dialog("You travel down the right path...","");
	set_flag(250,0,2);

	reset_dialog();
	add_dialog_str(0,"You are an apprentice mage, an outlaw, and a victim of circumstance.",0);
	add_dialog_str(1,"Before you were born the Empire exiled your mother and father. They didn't end up in Avernum, though; they fled and hid in a forest close to their home, living with outcasts just like them.",0);
	add_dialog_str(2,"Since your birth you have been forced to move from place to place, always trying to avoid the Empire. During the years on the run, you and your fellow castaways began to learn magic, growing stronger, greater in number, and increasingly bitter.",0);
	add_dialog_str(3,"Now, things are changing. The Emperor has announced that he will finally rid the Empire of all bandits. Having no choice but to fight, your group began attacking a small city, hoping to seize it as a fortified base of operations in the upcoming war."0);
	add_dialog_str(4,"You formulate a plan to kill the mayor of this town - New Haven - and his people, sending small sorties at first to lull them into a false sense of security. In just a few hours, you'll finally have a place to call home...",0);
	run_dialog(1);
	
	message_dialog("There is an earth-shattering explosion as your home-made bomb hits the outer wall of New Haven. Within seconds, people begin fleeing the area, only to come face-to-face with your allies at the town's gates. Trapped, they begin fleeing in all directions.","Your friend and leader, Steven, looks over at you. _Yes! It worked! Right, come here, quick! We don't have much time!_");
	message_dialog("(You may want to rename and change the graphic of your PC in order to reflect your status and skills, although this is in no way required of you. Also, note that any saved game entering this scenario is forfeit.)","");
	
//Now to prepare party --- kill extra PCs here and take items. 
		
//Firstly, lets kill off all unneeded pcs, and take their items...
//Lets explain what the heck I'm doing. To start with, I'm going to find the PC that I
//want to keep. So, I check to see if the first PC is dead, and if it is, check the next 
//one. If I find one that is alive, that character's number becomes the variable i. I can
//then use this to erase all other characters that don't match this number.
					
//Next, I have to take their items, and then kill them. For this, I need two more variables.
//I'm using the variable j to keep track of the players items. It starts at 39, since there
//are 39 item slots. I'm then going to erase each item slot in reverse order - it has to be
//done this way because BoA shifts items up when you delete one. 

//I'm also going to be using the variable k to compare PCs. Basically, I will go
//through each PC, and delete their items. Then, if they are not i (the PC we earmarked to
//save before), I'll erase them. 
	
	i = 0;
	k = 0;
	while(char_ok(i) == 0) {
		i = i + 1;
		}
				
	while(k <= 3) {
		j = 39;
		if(char_ok(k) == 1) {
			while(j >= 0) {
				destroy_char_item(k,j);
				j = j - 1;
			}
			if(k != i) {
				erase_char(k);
			}
		}
		k = k + 1;
	}
	
	set_flag(250,1,i); //Just make a note of which party member our survivor is.
	
//Now  to sort out our survivors level and exp. Then we can focus on stats and spells. 
//What I'm going to start by doing is setting a variable (level) to less than one. 
//This is so that I can change exp later. If I take away the current level, say 14 from
//1, I'm left with a figure of -13. I can then reduce exp for every level under 1, until
//I have removed every piece of experience earned. 

//I'm not going to mess about with all the crap about exp modifiers - i can never take 
//a party under 0 exp, so i may as well just take away a really big amount of exp, 
//regardless of everything. This works for all parties except god ones. 
	
	level = (1 - get_level(i));
	set_level(i,1);
				
	modifier = 100;
	while(level < 1) {
		change_char_xp(i,-25 * modifier);
		level = level + 1;
	}
	
//Removing all traits... 
				
	modifier = 0;
	while(modifier <= 22) {
		set_char_trait(i,modifier,0);
		modifier = modifier + 1;
	}
	
//Removing spells. I'll set what I want them to have later	
	
	modifier = 0;
	while(modifier <= 19) {
		if(get_spell_level(i,0,modifier) > 0)
			change_spell_level(i,0,modifier,-1 * get_spell_level(i,0,modifier));
		if(get_spell_level(i,1,modifier) > 0)
			change_spell_level(i,1,modifier,-1 * get_spell_level(i,1,modifier));
		modifier = modifier + 1;
	}
	
//Remove all their skill points - we don't want them to train!!		
	
	change_pc_skill_pts(i,-1 * (get_char_skill_pts(i)));	
	change_pc_skill_pts(i,-1 * (get_char_skill_pts(i)));

//And now stats. Using an old variable or two from earlier, we can go through each in
//turn and remove each level of every skill. Here j is the skill number, i is still the pc
// and modifier is how much of each stat the player has. 	
		
	j = 0;
	while (j < 32) {
		modifier = get_stat(i,j);
		alter_stat(i,j,-1 * modifier);
		j = j + 1;
	}
	

//Now I make them how I want them...	
		
	alter_stat(i,4,3); //Melee Weapons
	alter_stat(i,2,2); //Int
	alter_stat(i,6,1); //Bows
	alter_stat(i,11,6); //Mage Spells
	alter_stat(i,13,5); //Arcane Lore
	
//Add some spells
		
	change_spell_level(i,0,0,2);
	change_spell_level(i,0,1,2);
	change_spell_level(i,0,2,1);
	change_spell_level(i,0,3,1);
	change_spell_level(i,0,4,1);
	change_spell_level(i,0,5,1);
	
//Tidy up - give them starting equipment etc...	
	
	revive_party();
	change_coins(-1 * coins_amount()); //Make them poor!
	force_start_day(-1); //No calender, please - we're british
	reward_give(18);
	reward_give(450);
	reward_give(114);
	reward_give(46);
	
	
}
break;

beginstate START_STATE;
// This state is called every tick wherever the party is in the scenario.
// You can use the set_state

erase_text_bubbles();

break;
